#!/bin/bash
set -xeuo pipefail
set -o errexit

# ES 7.9才支持

#file:///hdd/test/built-docs-master/raw/en/elasticsearch/reference/current/set-up-a-data-stream.html
:<<EOF
Optional: Configure an ILM lifecycle policy.
Create an index template.
Create the data stream.
Secure the data stream.
EOF

curl -XDELETE http://localhost:9200/_data_stream/my-data-stream
curl -XDELETE http://localhost:9200/my-data-stream?pretty
curl -XDELETE http://localhost:9200/_ilm/policy/my-data-stream-policy?pretty 
curl -XDELETE http://localhost:9200/_index_template/my-data-stream-template?pretty 

curl -XPUT http://localhost:9200/_ilm/policy/my-data-stream-policy?pretty -H 'Content-Type: application/json' -d'
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_age":"30s"
          }
        }
      },
      "delete": {
        "min_age": "90s",           
        "actions": {
          "delete": {}
        }
      }
    }
  }
}
'
#If the index template doesn’t specify a mapping for the @timestamp field, Elasticsearch maps @timestamp as a date field with default options.
curl -XPUT http://localhost:9200/_index_template/my-data-stream-template?pretty -H 'Content-Type: application/json' -d' 
{
  "index_patterns": [ "my-data-stream*" ],
  "data_stream": { },
  "priority": 200,
  "template": {
    "settings": {
      "index.lifecycle.name": "my-data-stream-policy"
    }
  }
}
'

curl -XPUT http://localhost:9200/_data_stream/my-data-stream-alt
curl -XGET http://localhost:9200/_data_stream/my-data-stream*

curl -XPOST http://localhost:9200/my-data-stream/_doc/?pretty -H 'Content-Type: application/json' -d'
{
  "@timestamp": "2020-12-06T11:04:05.000Z",
  "user": {
    "id": "vlb44hny"
  },
  "message": "Login attempt failed"
}
'
